﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Sales_SaleProcSrh
 * 类型：       控制层
 * 功能说明：   销售过程检索页面
 * 作者：       黄鑫
 * 创建日期：   2007/08/26
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using log4net;
using Shpm.App.Search.Bean;
using Shpm.App.Search.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using Shpm.System;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Inp;

public partial class App_Search_SaleProcSrhForm : System.Web.UI.Page
{
    protected static ILog log = LogManager.GetLogger(typeof(App_Search_SaleProcSrhForm));
    private const string COMMAND_SEARCH = "DoSearch";
    private const int SALE_PROC_ID = 0;
    private const int SALE_STATUS = 1;
    private const int SALE_STEP = 2;

    //Form Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            initPageControls();

            if (Context.Items.Contains("EmployeeId") == false)
            {
                SetEmployeeId();
            }
            // 接收传递参数并初始化控件
            txtPersonId.Text = Convert.ToString(Context.Items["PersonId"]);
            if (Context.Items.Contains("EmployeeId"))
            {
                txtEmployeeId.Text = Convert.ToString(Context.Items["EmployeeId"]);
            }
            lstSaleStatus.SelectedIndex = Convert.ToInt32(Context.Items["SaleStatus_SelectedIndex"]);
            lstSaleStep.SelectedIndex = Convert.ToInt32(Context.Items["SaleStep_SelectedIndex"]);
            lstRelationIntent.SelectedIndex = Convert.ToInt32(Context.Items["RelationIntent_SelectedIndex"]);
            txtBeginDate.Text = Convert.ToString(Context.Items["BeginDate"]);
            txtEndDate.Text = Convert.ToString(Context.Items["EndDate"]);

            // 接收传递参数为检索条件
            if (Context.Items["ParamPersonId"] == null)
            {
                ViewState["ParamPersonId"] = null;
            }
            else
            {
                ViewState["ParamPersonId"] = Convert.ToInt32(Context.Items["ParamPersonId"]);
            }
            if (Context.Items["ParamCurrentStep"] == null)
            {
                ViewState["ParamCurrentStep"] = null;
            }
            else
            {
                ViewState["ParamCurrentStep"] = Convert.ToInt32(Context.Items["ParamCurrentStep"]);
            }
            ViewState["ParamEmployeeId"] = Convert.ToString(Context.Items["ParamEmployeeId"]);
            ViewState["ParamSaleStatus"] = Convert.ToString(Context.Items["ParamSaleStatus"]);
            if (Context.Items["ParamRelationIntent"] == null)
            {
                ViewState["ParamRelationIntent"] = null;
            }
            else
            {
                ViewState["ParamRelationIntent"] = Convert.ToInt32(Context.Items["ParamRelationIntent"]);
            }
            ViewState["ParamBeginDate"] = Convert.ToString(Context.Items["ParamBeginDate"]);
            ViewState["ParamEndDate"] = Convert.ToString(Context.Items["ParamEndDate"]);

            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }
        }

    }

    // Step Click
    protected void LinkButton_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Length > 4)
        {
            if (e.CommandName.Substring(0, 4) == "Step")
            {
                Context.Items["SALE_PROC_ID"] = Convert.ToInt32(e.CommandArgument);
                Context.Items["ParamSaleProcId"] = Convert.ToInt32(e.CommandArgument);

                // 传递分页编号
                Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;

                // 传递控件参数
                Context.Items["PersonId"] = txtPersonId.Text;
                Context.Items["EmployeeId"] = txtEmployeeId.Text;
                Context.Items["SaleStatus_SelectedIndex"] = lstSaleStatus.SelectedIndex;
                Context.Items["SaleStep_SelectedIndex"] = lstSaleStep.SelectedIndex;
                Context.Items["RelationIntent_SelectedIndex"] = lstRelationIntent.SelectedIndex;
                Context.Items["BeginDate"] = txtBeginDate.Text;
                Context.Items["EndDate"] = txtEndDate.Text;

                // 传递检索条件参数
                Context.Items["ParamPersonId"] = ViewState["ParamPersonId"];
                Context.Items["ParamEmployeeId"] = ViewState["ParamEmployeeId"];
                Context.Items["ParamSaleStatus"] = ViewState["ParamSaleStatus"];
                Context.Items["ParamCurrentStep"] = ViewState["ParamCurrentStep"];
                Context.Items["ParamRelationIntent"] = ViewState["ParamRelationIntent"];
                Context.Items["ParamBeginDate"] = ViewState["ParamBeginDate"];
                Context.Items["ParamEndDate"] = ViewState["ParamEndDate"];

                ControllerUtils.Transfer(this.Context, "~/App/Input/SaleCountForm.aspx", true);

                /*
                if (e.CommandName == "Step0")
                {
                    //过程开始
                    //Server.Transfer("../Input/SaleProcessBeginForm.aspx", true);
                    ControllerUtils.Transfer(this.Context, "~/App/Input/SaleProcessBeginForm.aspx", true);
                }
                if (e.CommandName == "Step1")
                {
                    //过程开始
                    //Server.Transfer("../Input/SaleProcesseMembership.aspx", true);
                    ControllerUtils.Transfer(this.Context, "~/App/Input/MemberForm.aspx", true);
                }
                else if (e.CommandName == "Step2")
                {
                    //体检报告
                    //Server.Transfer("../Input/SaleProcessHealthForm.aspx", true);
                    ControllerUtils.Transfer(this.Context, "~/App/Input/SaleProcessHealthForm.aspx", true);
                }
                else if (e.CommandName == "Step3")
                {
                    //资格审核
                    //Server.Transfer("../Input/SaleProcessIndefinForm.aspx", true);
                    ControllerUtils.Transfer(this.Context, "~/App/Input/SaleProcessIndefinForm.aspx", true);
                }
                else if (e.CommandName == "Step4")
                {
                    //合同签订
                    //Server.Transfer("../Input/ContractUpdForm.aspx", true);
                    ControllerUtils.Transfer(this.Context, "~/App/Input/ContractUpdForm.aspx", true);
                }
                else if (e.CommandName == "Step5")
                {
                    //费用缴纳
                    //Server.Transfer("../Input/PaymentUpdForm.aspx", true);
                    ControllerUtils.Transfer(this.Context, "~/App/Input/PaymentUpdForm.aspx", true);
                }
                else if (e.CommandName == "Step6")
                {
                    //礼品发放
                    //Server.Transfer("../Input/SaleProcessPresentForm.aspx", true);
                    //ControllerUtils.Transfer(this.Context, "~/App/Search/SaleProcSrhForm.aspx", true);
                }
                */
            }
        }
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // bind common catalog data 
    private void initPageControls()
    {
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        IList lstCat = null;

        // 联系人意向
        log.Fatal("\n ***** get T_SALES_PROC.INTENT catalog *****");
        beanComDict.Catalog = "T_SALES_PROC.INTENT";
        lstCat = dao.FindByCatalog(beanComDict);
        lstRelationIntent.DataSource = lstCat;
        lstRelationIntent.DataBind();

        foreach (CommonDictionaryBean bean in lstCat)
        {
            switch (bean.DictNo)
            {
                case "1":
                    lblXY1.Text = bean.Content.ToString() + "：　";
                    break;
                case "2":
                    lblXY2.Text = bean.Content.ToString() + "：　";
                    break;
                case "3":
                    lblXY3.Text = bean.Content.ToString() + "：　";
                    break;
                case "4":
                    lblXY4.Text = bean.Content.ToString() + "：　";
                    break;
                case "5":
                    lblXY5.Text = bean.Content.ToString() + "：　";
                    break;
                default:
                    break;
            }
        }

        /* lstRelationIntent.Items.Clear();
         lstRelationIntent.Items.Add("");
         foreach (CommonDictionaryBean bean in lstCat)
         {
             lstRelationIntent.Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
         }*/
    }


    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (COMMAND_SEARCH.Equals(ViewState["ParamCommand"]))
        {
            SaleProcSrhBean dataBean = new SaleProcSrhBean();
            if (ViewState["ParamPersonId"] == null)
            {
                dataBean.PersonId = null;
            }
            else
            {
                dataBean.PersonId = Convert.ToInt32(ViewState["ParamPersonId"]);
            }
            if (ViewState["ParamEmployeeId"] == null)
            {
                dataBean.EmployeeId = null;
            }
            else
            {
                dataBean.EmployeeId = Convert.ToString(ViewState["ParamEmployeeId"]);
            }
            if (ViewState["ParamCurrentStep"] == null)
            {
                dataBean.CurrentStep = null;
            }
            else
            {
                dataBean.CurrentStep = Convert.ToInt32(ViewState["ParamCurrentStep"]);
            }

            dataBean.RelationName = ViewState["ParamRelationName"] == null ? null : Convert.ToString(ViewState["ParamRelationName"]);
            dataBean.SaleStatus = ViewState["ParamSaleStatus"] == null ? null : Convert.ToString(ViewState["ParamSaleStatus"]);
            dataBean.RelationIntent = ViewState["ParamRelationIntent"] == null ? null : Convert.ToString(ViewState["ParamRelationIntent"]);
            dataBean.BeginDate = ViewState["ParamBeginDate"] == null ? null : Convert.ToString(ViewState["ParamBeginDate"]);
            dataBean.EndDate = ViewState["ParamEndDate"] == null ? null : Convert.ToString(ViewState["ParamEndDate"]);
            SaleProcSrhDao dao = new SaleProcSrhDao();
            IList dataList = dao.ListSaleProcSrh(dataBean);
            IList dataCntList = dao.ListSaleProcSrhCount(dataBean);
            viewSrhRst.DataSource = dataList;
            if (dataList.Count == 0)
            {
                ((Design_MasterPage)this.Master).InfoMessage =
                    HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0005").ToString();
                tblResult.Visible = false;
            }
            else
            {
                tblResult.Visible = true;
            }
            Setlbl(dataCntList);
            viewSrhRst.DataBind();
        }
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            /* if (e.Row.Cells[SALE_STATUS].Text == "0")
             {
                 e.Row.Cells[SALE_STATUS].Text = "未洽谈";
                
             }
             else if (e.Row.Cells[SALE_STATUS].Text == "1")
             {
                 e.Row.Cells[SALE_STATUS].Text = "洽谈中";
             }
             else if (e.Row.Cells[SALE_STATUS].Text == "2")
             {
                 e.Row.Cells[SALE_STATUS].Text = "洽谈成功";
             }
             else if (e.Row.Cells[SALE_STATUS].Text == "3")
             {
                 e.Row.Cells[SALE_STATUS].Text = "洽谈结束";
             }*/
            int i = e.Row.DataItemIndex % viewSrhRst.PageSize;
            DataKey key = viewSrhRst.DataKeys[i];
            e.Row.Cells[4].Text = strStatus(Convert.ToInt32(key.Value));
            int step = (((SaleProcSrhBean)e.Row.DataItem).CurrentStep == null) ? 0 : ((SaleProcSrhBean)e.Row.DataItem).CurrentStep.Value;
            if (step == 0)
            {
                //e.Row.Cells[SALE_STEP].Text = "尚未开始";
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = true;
            }
            else if (step == 1)
            {
                //e.Row.Cells[SALE_STEP].Text = "入会登记";
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step2")).Enabled = true;
            }
            else if (step == 2)
            {
                //e.Row.Cells[SALE_STEP].Text = "体检报告";
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step2")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step3")).Enabled = true;
            }
            else if (step == 3)
            {
                //e.Row.Cells[SALE_STEP].Text = "资格审核";
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step2")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step3")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step4")).Enabled = true;
            }
            else if (step == 4)
            {
                //e.Row.Cells[SALE_STEP].Text = "合同签订";
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step2")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step3")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step4")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step5")).Enabled = true;
            }
            else if (step == 5)
            {
                //e.Row.Cells[SALE_STEP].Text = "费用缴纳";
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step2")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step3")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step4")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step5")).Enabled = true;
            }
            else if (step == 6)
            {
                //e.Row.Cells[SALE_STEP].Text = "礼品发放";
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step2")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step3")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step4")).Enabled = true;
                ((LinkButton)e.Row.FindControl("Step5")).Enabled = true;
                //((LinkButton)e.Row.FindControl("Step6")).Enabled = true;
            }
            ((LinkButton)e.Row.FindControl("Step0")).CommandArgument = Convert.ToString(((Shpm.App.Search.Bean.SaleProcSrhBean)e.Row.DataItem).SaleProcId);
            ((LinkButton)e.Row.FindControl("Step1")).CommandArgument = Convert.ToString(((Shpm.App.Search.Bean.SaleProcSrhBean)e.Row.DataItem).SaleProcId);
            ((LinkButton)e.Row.FindControl("Step2")).CommandArgument = Convert.ToString(((Shpm.App.Search.Bean.SaleProcSrhBean)e.Row.DataItem).SaleProcId);
            ((LinkButton)e.Row.FindControl("Step3")).CommandArgument = Convert.ToString(((Shpm.App.Search.Bean.SaleProcSrhBean)e.Row.DataItem).SaleProcId);
            ((LinkButton)e.Row.FindControl("Step4")).CommandArgument = Convert.ToString(((Shpm.App.Search.Bean.SaleProcSrhBean)e.Row.DataItem).SaleProcId);
            ((LinkButton)e.Row.FindControl("Step5")).CommandArgument = Convert.ToString(((Shpm.App.Search.Bean.SaleProcSrhBean)e.Row.DataItem).SaleProcId);
            ((LinkButton)e.Row.FindControl("Step6")).CommandArgument = Convert.ToString(((Shpm.App.Search.Bean.SaleProcSrhBean)e.Row.DataItem).SaleProcId);

            if (((SaleProcSrhBean)e.Row.DataItem).RelationIntentCount != 5)
            {
                ((LinkButton)e.Row.FindControl("Step1")).Enabled = false;
                ((LinkButton)e.Row.FindControl("Step2")).Enabled = false;
                ((LinkButton)e.Row.FindControl("Step3")).Enabled = false;
                ((LinkButton)e.Row.FindControl("Step4")).Enabled = false;
                ((LinkButton)e.Row.FindControl("Step5")).Enabled = false;
                ((LinkButton)e.Row.FindControl("Step6")).Enabled = false;
            }
        }

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        MainMenuManager manager = new MainMenuManager(Context);
        MainMenuBean menuBean = manager.GetCurrentMenu();

        manager.Forward(menuBean);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (comValidatSrh.DoValidator())
        {
            // 保存控件状态为检索条件
            if (txtPersonId.Text == "")
            {
                ViewState["ParamPersonId"] = null;
            }
            else
            {
                ViewState["ParamPersonId"] = Convert.ToInt32(txtPersonId.Text);
            }

            if (txtEmployeeId.Text == "")
            {
                ViewState["ParamEmployeeId"] = null;
            }
            else
            {
                ViewState["ParamEmployeeId"] = Convert.ToString(txtEmployeeId.Text);
            }

            if (lstSaleStep.SelectedValue == "")
            {
                ViewState["ParamCurrentStep"] = null;
            }
            else
            {
                ViewState["ParamCurrentStep"] = Convert.ToInt32(lstSaleStep.SelectedValue);
            }

            ViewState["ParamRelationName"] = txtRelationName.Text == "" ? null : txtRelationName.Text;
            ViewState["ParamSaleStatus"] = lstSaleStatus.Text == "" ? null : lstSaleStatus.SelectedValue;
            ViewState["ParamRelationIntent"] = "0".Equals(lstRelationIntent.SelectedValue) ? null : lstRelationIntent.SelectedValue;
            ViewState["ParamBeginDate"] = txtBeginDate.Text == "" ? null : txtBeginDate.Text;
            ViewState["ParamEndDate"] = txtEndDate.Text == "" ? null : txtEndDate.Text;
            ViewState["ParamCommand"] = COMMAND_SEARCH;
            viewSrhRst.PageIndex = 0;
        }
    }
    //设置职员编号
    private void SetEmployeeId()
    {
        SysUserManager manager = new SysUserManager(Context);
        MstEmployeeBean empBean = manager.getSysUserBean().getEmployeeBean();
        if (empBean != null && empBean.Role != Constants.ROLE_SALE_MANAGER)
        {
            ViewState["EmployeeId"] = empBean.EmployeeId;
            Context.Items["ParamEmployeeId"] = empBean.EmployeeId;
        }
        else
        {
            ViewState["EmployeeId"] = string.Empty;
        }
        txtEmployeeId.Text = (string)ViewState["EmployeeId"];
        ViewState["ParamCommand"] = COMMAND_SEARCH;
    }

    //显示统计
    private void Setlbl(IList lst)
    {
        lblCnt5.Text = "－";
        lblCnt4.Text = "－";
        lblCnt3.Text = "－";
        lblCnt2.Text = "－";
        lblCnt1.Text = "－";

        foreach (SaleProcSrhBean dataBean in lst)
        {
            switch (dataBean.RelationIntent)
            {
                case "1":
                    lblCnt1.Text = dataBean.RelationIntentCount.ToString();
                    break;
                case "2":
                    lblCnt2.Text = dataBean.RelationIntentCount.ToString();
                    break;
                case "3":
                    lblCnt3.Text = dataBean.RelationIntentCount.ToString();
                    break;
                case "4":
                    lblCnt4.Text = dataBean.RelationIntentCount.ToString();
                    break;
                case "5":
                    lblCnt5.Text = dataBean.RelationIntentCount.ToString();
                    break;
                default:
                    break;
            }
        }
    }
    protected string strStatus(int procId)
    {

        //客户移交
        InpCheckInTaskBean paramBean = new InpCheckInTaskBean();
        paramBean.SaleProcId = procId;
        SaleFinishDao finishDao = new SaleFinishDao();
        IList SaleFinshList = finishDao.FindByCheckInTask(paramBean);
        //资格审核
        SaleProcessDao indefinDao = new SaleProcessDao();
        Shpm.App.Input.Bean.SaleProcessBean indefinBean = new Shpm.App.Input.Bean.SaleProcessBean();
        indefinBean.SaleProcId = procId;
        indefinBean = indefinDao.FindBySaleProcess(indefinBean);
        //体检报告

        //入住资料、购卡阶段
        SaleProcessDao spDao = new SaleProcessDao();
        Shpm.App.Input.Bean.SaleProcessBean personBean = new Shpm.App.Input.Bean.SaleProcessBean();
        personBean.SaleProcId = procId;
        personBean = spDao.FindBySaleProcess(personBean);
        //

        //客户移交
        if (SaleFinshList.Count > 0)
        {
            return "入住移交";
        }
        //资格审核
        else if (indefinBean != null && indefinBean.CancelFlg)
        {
            return "资格审核";
        }
        //体检报告

        //入住资料
        else if (personBean.PersonId != null || personBean.SecondaryPersonId != null || personBean.AccompanyPersonId != null)
        {
            return "入住资料";
        }
        //购卡阶段
        else if (personBean.OrganizationId != null || personBean.RightPersonId != null)
        {
            return "购卡阶段";
        }
        //预销售中
        else
        {
            return "预销售中";
        }

    }
}
